#include "exynos_4412.h"

#define GPA1 (* (volatile gpa1 *)0x11400020)
#define UART2 (* (volatile uart2 *)0x13820000)

void UART2_Init(){
	/*1.将GPA1_0和GPA1_1分别设置成UART2的接收引脚和发送引脚 GPA1CON[7:0]*/
	GPA1.CON = GPA1.CON & (~(0xFF)) | (0x22);
	/*2.设置UART2的帧格式 ULCON2 / 8位数据位 1位停止位 无校验 正常模式*/
	UART2.ULCON2 = UART2.ULCON2 & (~(0x7F)) | (0x3);
	/*3.设置UART2的接收和发送模式为轮训模式 UCON2[3:0]*/
	UART2.UCON2 = UART2.UCON2 & (~(0xF)) | (0x5);
	/*4.设置UART的波特率为115200 UBRDIV2/UFRACVAL2*/
	UART2.UBRDIV2 = 53;
	UART2.UFRACVAL2 = 4;
}
void LED_Init(void){
	GPX2.CON = GPX2.CON & (~(0xF) << 28) | (0x1 << 28);
	GPX1.CON = GPX1.CON & (~(0xF) << 28) | (0x1 << 28);
	GPF3.CON = GPF3.CON & (~(0xF) << 16) | (0x1 << 16);
	GPF3.CON = GPF3.CON & (~(0xF) << 20) | (0x1 << 20);
}

void UART2_Send_Byte(char Date){
	/*等待发送寄存器，若寄存器为空，即上一个数据已经发完，则接收下一个数据*/	
	while(!(UART2.UTRSTAT2 & (1 << 1)));
	/*将要发送的数据写入发送寄存器*/
	UART2.UTXH2 = Date;
}

char UART2_Rece_Byte(void){
	char Date;
	/*判断接收寄存器是否接收到了数据*/
	if(UART2.UTRSTAT2 & 1){
		Date = UART2.URXH2;
		return Date;
	}
	else{
		return 0;
	}
}

int main()
{
	char RecDate = 0;
	UART2_Init();
	LED_Init();

	while(1){
		RecDate = UART2_Rece_Byte();
		if(RecDate == 0){

		}
		else{
			//RecDate = RecDate + 1;
			UART2_Send_Byte(RecDate);
			if(RecDate == 2){
				GPX2.DAT = GPX2.DAT | (1 << 7);
				UART2_Send_Byte(RecDate);
				if(RecDate == 2){
					GPX2.DAT = GPX2.DAT & (~(1 << 7));
				}
			}else if(RecDate == 3){
				GPX1.DAT = GPX1.DAT | (1 << 7);
				UART2_Send_Byte(RecDate);
				if(RecDate == 3){
					GPX1.DAT = GPX1.DAT & (~(1 << 7));
				}
			}else if(RecDate == 4){
				GPF3.DAT = GPF3.DAT | (1 << 4);
				UART2_Send_Byte(RecDate);
				if(RecDate == 4){
					GPF3.DAT = GPF3.DAT & (~(1 << 4));
				}
			}else if(RecDate == 5){
				GPF3.DAT = GPF3.DAT | (1 << 5);
				UART2_Send_Byte(RecDate);
				if(RecDate == 5){
					GPF3.DAT = GPF3.DAT & (~(1 << 5));
				}
			}else
				return RecDate;
		}
	}
	return 0;
}


	//
	// 00000000 00000000 00100010 
	// 清0
	// 11111111 11111111 00000000
	// 00000000 00000000 11111111 0xFF
	// 置1 
	// 00000000 00000000 00100010 0x22 
	
	// 00000000 00000000 00000000 00000011
	// 清0
	// 11111111 11111111 11111111 11110000
	// 00000000 00000000 00000000 00001111 0xF
	// 置1 
	// 00000000 00000000 00000000 00000011 0x3
	
	// 00000000 00000000 00000000 00000101
	// 清0
	// 11111111 11111111 11111111 11110000
	// 00000000 00000000 00000000 00001111 0xF
	// 置1 
	// 00000000 00000000 00000000 00000101 0x5
